package com.tencent.mlvb.apilive.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.tencent.mlvb.apilive.ExitApplication;
import com.tencent.mlvb.apilive.bean.LivePlanBean;
import com.tencent.mlvb.apilive.network.ApiRequestService;
import com.tencent.mlvb.apilive.network.CallBackLis;
import com.tencent.mlvb.apilive.network.HttpCall;
import com.tencent.mlvb.apilive.network.ResponseModel;
import com.tencent.mlvb.apilive.network.RetrofitHelper;
import com.tencent.mlvb.apilive.utils.Md5Util;
import com.tencent.mlvb.apilive.databinding.ActivityLiveInfoBinding;
import com.tencent.mlvb.apilive.utils.SpUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.Observable;

public class LiveInfoActivity extends AppCompatActivity {
    private ActivityLiveInfoBinding binding;
    private String code;
    private String uavId;
    private LivePlanBean livePlanBean;
    private boolean isLive;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding = ActivityLiveInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ExitApplication.getInstance().addActivity(this);
        initView();
        initData();
        isLive = SpUtils.getBoolean(getApplicationContext(), "isLive", false);
    }

    private void initData(){
        //获取未完成直播
         code = getIntent().getStringExtra("code");
         uavId = getIntent().getStringExtra("uavId");
        getIncomplete(code,uavId);
    }
    private void initView(){
        binding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //取消直播
                if (isLive){
                    Toast.makeText(LiveInfoActivity.this, "当前正在直播,请先结束直播", Toast.LENGTH_SHORT).show();
                    return;
                }
                cancelLive();
            }
        });
        binding.tvContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLive){
                    Toast.makeText(LiveInfoActivity.this, "当前正在直播", Toast.LENGTH_SHORT).show();
                    return;
                }
                //继续直播任务,直接进入直播准备
                Intent intent = new Intent(LiveInfoActivity.this, LivePrepareActivtiy.class);
                intent.putExtra("type",1);
                intent.putExtra("code",code);
                intent.putExtra("uavId",uavId);
                intent.putExtra("livePlanBean",livePlanBean);
                startActivity(intent);
            }
        });
    }

    private void getIncomplete(String code,String uavId){
        Map<String,String> hashMap = new HashMap();
        hashMap.put("code", code);
        hashMap.put("uavId", uavId);
        String mapToMD5String = Md5Util.getMapToMD5String(hashMap);

        Observable<ResponseModel<LivePlanBean>> observable = RetrofitHelper.getInstance().getRetrofit().create(ApiRequestService.class).incomplete(code,uavId,mapToMD5String);
        HttpCall.doCall(this, observable, new CallBackLis<LivePlanBean>(){
            @Override
            public void onSuccess(String method, LivePlanBean bean) {
                if (bean == null){
                    //没有未完成直播任务,直接进入下一个界面
                    Intent intent = new Intent(LiveInfoActivity.this, LivePrepareActivtiy.class);
                    intent.putExtra("type",0);
                    intent.putExtra("code",code);
                    intent.putExtra("uavId",uavId);
                    startActivity(intent);
                    finish();
                }else {
                    livePlanBean = bean;
                    binding.tvLiveInfo.setText("当前有未完成的直播，信息如下：\n" + livePlanBean.getStreamName() +" 公里数"+livePlanBean.getMileage());

                }
            }

            @Override
            public void onFailure(String method, String error) {
                Log.e("LiveInfoActivity 请求错误",error);
            }
        }, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean  isLive1 = SpUtils.getBoolean(getApplicationContext(), "isLive", false);
        if (isLive1){
            moveTaskToBack(true);
        }
    }

    private void cancelLive(){
        if (livePlanBean == null){
            Intent intent = new Intent(LiveInfoActivity.this, LivePrepareActivtiy.class);
            intent.putExtra("type",0);
            intent.putExtra("code",code);
            intent.putExtra("uavId",uavId);
            startActivity(intent);
            finish();
            return;
        }
        Map<String,String> hashMap = new LinkedHashMap<>();
        hashMap.put("id", code);
        String mapToMD5String = Md5Util.getMapToMD5String(hashMap);

        Observable<ResponseModel<Object>> observable = RetrofitHelper.getInstance().getRetrofit().create(ApiRequestService.class).deleteLive(livePlanBean.getId()+"",mapToMD5String);
        HttpCall.doCall(LiveInfoActivity.this, observable, new CallBackLis<Object>() {
            @Override
            public void onSuccess(String method, Object content) {
                Intent intent = new Intent(LiveInfoActivity.this, LivePrepareActivtiy.class);
                intent.putExtra("type",0);
                intent.putExtra("code",code);
                intent.putExtra("uavId",uavId);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(String method, String error) {

            }
        }, null);
    }
}
